<?php


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class HelpDeskController extends HelpDeskControllerBase 
{
	/**
	 * (non-PHPdoc)
	 * @see v1_00/com_helpdesk/site/HelpDeskControllerBase#display($cachable)
	 */
	function display() 
	{
		// create plugin dispatcher & execute published plugins
		$dispatcher	= &HelpDeskHelperPlugin::getInstance();
		$dispatcher->trigger('onBeforeDisplayComponentHelpDesk', array() );
	
			parent::display();
		
		// create plugin dispatcher & execute published plugins
		$dispatcher	= HelpDeskHelperPlugin::getInstance();
		$dispatcher->trigger('onAfterDisplayComponentHelpDesk', array() );
		
		$config = &HelpDeskConfig::getInstance();
		$show_linkback = $config->get( 'show_linkback', '1' );
		if ($show_linkback == '1') {
			// show a generous linkback, TIA
			echo "<p><center>";
			echo HelpDeskText::_( 'Powered by' )." <a href='http://www.dioscouri.com' target='_blank'>".HelpDeskText::_( 'HelpDesk' )."</a>";
			echo "</center></p>";
		}

	}

	/**
	 * 
	 * @return 
	 */
	function doTask()
	{
		$success = true;
		$msg = new stdClass();
		$msg->message = '';
		$msg->error = '';
				
		// expects $element in URL and $elementTask
		$element = HelpDeskRequest::getVar( 'element', '', 'request', 'string' );
		$elementTask = HelpDeskRequest::getVar( 'elementTask', '', 'request', 'string' );

		$msg->error = '1';
		// $msg->message = "element: $element, elementTask: $elementTask";
		
		// gets the plugin named $element
		$import 	= HelpDeskHelperPlugin::importPlugin( strtolower( 'HelpDesk' ), $element );
		$dispatcher	= HelpDeskHelperPlugin::getInstance();
		// executes the event $elementTask for the $element plugin
		// returns the html from the plugin
		// passing the element name allows the plugin to check if it's being called (protects against same-task-name issues)
		$result 	= $dispatcher->trigger( $elementTask, array( $element ) );
//		echo HelpDeskConfig::dump( $result );
//		echo HelpDeskConfig::dump( $dispatcher );
//		echo HelpDeskConfig::dump( $import );
		// This should be a concatenated string of all the results, 
			// in case there are many plugins with this eventname 
			// that return null b/c their filename != element) 
		$msg->message = implode( '', $result );
			// $msg->message = @$result['0'];
						
		// encode and echo (need to echo to send back to browser)		
		echo $msg->message;
		$success = $msg->message;

		return $success;
	}
	
	/**
	 * 
	 * @return 
	 */
	function doTaskAjax()
	{
		$success = true;
		$msg = new stdClass();
		$msg->message = '';
				
		// get elements $element and $elementTask in URL 
			$element = HelpDeskRequest::getVar( 'element', '', 'request', 'string' );
			$elementTask = HelpDeskRequest::getVar( 'elementTask', '', 'request', 'string' );
			
		// get elements from post
			// $elements = json_decode( preg_replace('/[\n\r]+/', '\n', HelpDeskRequest::getVar( 'elements', '', 'post', 'string' ) ) );
			
		// for debugging
			// $msg->message = "element: $element, elementTask: $elementTask";

		// gets the plugin named $element
			$import 	= HelpDeskHelperPlugin::importPlugin( strtolower( 'HelpDesk' ), $element );
			$dispatcher	= HelpDeskHelperPlugin::getInstance();
			
		// executes the event $elementTask for the $element plugin
		// returns the html from the plugin
		// passing the element name allows the plugin to check if it's being called (protects against same-task-name issues)
			$result 	= $dispatcher->trigger( $elementTask, array( $element ) );
		// This should be a concatenated string of all the results, 
			// in case there are many plugins with this eventname 
			// that return null b/c their filename != element)
			$msg->message = implode( '', $result );
			// $msg->message = @$result['0'];

		// set response array
			$response = array();
			$response['msg'] = $msg->message;
			
		// encode and echo (need to echo to send back to browser)
			echo ( json_encode( $response ) );

		return $success;
	}
}
?>